package com.proyecto.track.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.proyecto.track.bo.Project;
import com.proyecto.track.bo.ProjectStatus;
import com.proyecto.track.bo.ProjectUser;
import com.proyecto.track.bo.UserPerson;
import com.proyecto.track.dao.ProjectDAO;
import com.proyecto.track.dao.ProjectStatusDAO;

@Transactional(rollbackFor=Exception.class)
public class ProjectService {
	
	private ProjectDAO projectDAOImp;
	private ProjectStatusDAO projectStatusDAOImp;

	public ProjectStatusDAO getProjectStatusDAO() {
		return projectStatusDAOImp;
	}

	public void setProjectStatusDAO(ProjectStatusDAO projectStatusDAO) {
		this.projectStatusDAOImp = projectStatusDAO;
	}

	public ProjectDAO getProjectDAO() {
		return projectDAOImp;
	}

	public void setProjectDAO(ProjectDAO projectDAO) {
		this.projectDAOImp = projectDAO;
	}
	
	public Long createProject(Project project){
			if(project.getId() == null){
			System.out.println("Se crea un proyecto nuevo");
			return projectDAOImp.createProject(project);	
		}else{
			System.out.println("Se modifica el proyecto: " + project.getId());
			return projectDAOImp.modifyProject(project);
		}
		
	}
	
	public Long deleteProject(long id){
		return projectDAOImp.deleteProject(id);
	}
	
	public Long modifyProject(Project project){
		return projectDAOImp.modifyProject(project);
	}
	
	public List<ProjectStatus> searchProjectStatus(){
		return projectStatusDAOImp.searchProjectStatus();
	}
	
	public List<Project> searchProjects(){
		return projectDAOImp.searchProjects();
	}
	
	public List<Project> searchProjectsByAccountId(Long id){
		return projectDAOImp.searchProjectsByAccountId(id);
	}
	
	public Project searchProjects(Long id){
		return projectDAOImp.searchProjects(id);
	}
	
	public List<Project> searchProjects(String text){
		return projectDAOImp.searchProjects(text);
	}
	
	public Long newProjectStatus(ProjectStatus projectStatus){
		return projectStatusDAOImp.createProjectStatus(projectStatus);
		
	}

	public List<UserPerson> getUsers(Long id) {
		Project project = this.searchProjects(id);
		List<UserPerson> users = new ArrayList<UserPerson>();
		
		for (ProjectUser projectUser : project.getProjectUsers()) {
			users.add(projectUser.getUserPerson());
		}

		return users;
	}

	public void modify(long id, List<UserPerson> newUsersInProject) {
		
		Project project = projectDAOImp.searchProjects(id);
		
		List<UserPerson> oldUsers = this.getUsersActive(id);
		List<UserPerson> changeStatusUsers = this.getUsersActive(id);
		
		changeStatusUsers.removeAll(newUsersInProject);
		newUsersInProject.removeAll(oldUsers);
				
		for (UserPerson userPerson : changeStatusUsers) {
			project.removeUser(userPerson.getProjectUsersById(id));
		}
		
		for (UserPerson userPerson : newUsersInProject) {
			project.addUsers(userPerson);
			
		}
	}

	public List<UserPerson> getUsersActive(long id) {
		
		Project project = this.searchProjects(id);
		List<UserPerson> users = new ArrayList<UserPerson>();
		
		for (ProjectUser projectUser : project.getProjectUsers()) {
			if (projectUser.isActive()){
			users.add(projectUser.getUserPerson());
			}
		}

		return users;
	}


}
